/**
 * 一些正则表达式定义
 * @type {Readonly<{rex_alphabets: RegExp, rex_china: RegExp, rex_url: RegExp, rex_tel: RegExp, rex_phone: RegExp, rex_email: RegExp, rex_number: RegExp, rex_id_card: RegExp, rex_ip: RegExp}>}
 */
export const regexp_pattern = Object.freeze({
  rex_number: /^[0-9]*$/,
  rex_alphabets: /^[A-Za-z]+$/,
  rex_china: /^[\u4E00-\u9FA5]{2,4}$/,
  rex_phone: /^1[3|4|5|7|8|9][0-9]\d{8}$/,
  rex_tel: /^(400|800)([0-9\\-]{7,10})|(([0-9]{4}|[0-9]{3})(-| )?)?([0-9]{7,8})((-| |转)*([0-9]{1,4}))?$/,
  rex_email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
  rex_ip: /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
  rex_id_card: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
  rex_url: /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
})

/**
 * 基于正则表达匹配
 * @param {string} url
 * @returns {Boolean}
 */
export function isRegexp(regexp, content) {
  return regexp.test(content)
}

/**
 * 基于正则表达校验
 * @param pattern
 * @param value
 * @param message
 * @param callback
 * @returns {*}
 */
export function validRegexp(regexp, value, message, callback) {
  if (!regexp.test(value)) {
    return callback(new Error(message));
  } else {
    return callback()
  }
}

